// 可写流
const fs = require('fs');
const path = require('path');
const WriteStream = require('./WriteStream');

/* const ws = fs.createWriteStream(path.resolve(__dirname, '1.txt'), {
  flags: 'w',
  encoding: 'utf8',
  autoClose: true,
  highWaterMark: 2,
}); */
const ws = new WriteStream(path.resolve(__dirname, '1.txt'), {
  flags: 'w',
  encoding: 'utf8',
  autoClose: true,
  highWaterMark: 2,
});

ws.on('open', function (fd) {
  console.log(fd, '-----');
});

let flag = ws.write('z');
console.log(flag);
flag = ws.write('f');

console.log(flag);

ws.on('drain', () => {
  console.log('干了'); // 必须达到预期，并且消耗掉
});

setTimeout(() => {
  flag = ws.write('a');

  console.log(flag); // true
}, 1000);

/**
 * 1. 格式化传入的数据，默认需要打开文件
 * 2. 用户调用write方法，Writable接口实现了write方法内部都会调用_write方法，fs.write方法
 * 3. 区分是第一次写人，还是
 */
